using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces;


namespace FiltronASP.Helpers.Routes
{
    /// <summary>
    /// Klasa pomocnicza do generowania linkow na podstawie mapowan zawartych w klasie zarejestrowanych routow - menu.
    /// </summary>
    public static class MenuRoutesHelper
    {

        /// <summary>
        /// Klasa pomocnicza do generowania linkow zwiazanych z routem <code>MenusList</code>.
        /// </summary>
        /// <param name="urlHelper">url helper z c#</param>
        /// <param name="menu">atualne menu</param>
        /// <param name="language">aktualna wersja jezykowa</param>
        /// <returns></returns>
        public static string GenerateMenusListRoute(this UrlHelper urlHelper, Menu menu, Language language)
        {
            int page = 1;
            if (menu == null)
            {
                MenuDAO menuDAO = new MenuDAO();
                menu = menuDAO.GetDefaultMenu(language);
            }   //if page type
            if ((language != null) && (menu != null))
            {
                return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
            }
            return null;

        }

        /// <summary>
        /// Klasa pomocnicza do generowania linkow zwiazanych z routem <code>MenusList</code>.
        /// </summary>
        /// <param name="urlHelper">url helper z c#</param>
        /// <param name="menu">atualne menu</param>
        /// <param name="language">aktualna wersja jezykowa</param>
        /// <returns></returns>
        public static string GenerateMenusListRoute(this UrlHelper urlHelper, Menu menu, MenuItem menuItem,
            Language language)
        {
            int page = 1;
            if (menu == null)
            {
                MenuDAO menuDAO = new MenuDAO();
                menu = menuDAO.GetDefaultMenu(language);
            }   //if menu 
            int menuItemId = 0;
            if (menuItem != null)
            {
                menuItemId = menuItem.item_id;
            }   //if page type
            if (menuItemId != 0)
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusList", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id), menuItemId = (menuItem.item_id) });
                }
            }
            else
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
                }
            }
            return null;
        }

        /// <summary>
        /// Klasa pomocnicza do generowania linkow zwiazanych z routem <code>MenusList</code>.
        /// </summary>
        /// <param name="urlHelper">url helper z c#</param>
        /// <param name="menu">atualne menu</param>
        /// <param name="menuItem">biezacy parent</param>
        /// <param name="page">strona w paginacji</param>
        /// <param name="language">aktualna wersja jezykowa</param>
        /// <returns></returns>
        public static string GenerateMenusListRoute(this UrlHelper urlHelper, int? page, Menu menu,
            MenuItem menuItem, Language language)
        {
            if (menu == null)
            {
                MenuDAO menuDAO = new MenuDAO();
                menu = menuDAO.GetDefaultMenu(language);
            }   //if menu 
            int menuItemId = 0;
            if (menuItem != null)
            {
                menuItemId = menuItem.item_id;
            }   //if page type
            if (menuItemId != 0)
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusList", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id), menuItemId = (menuItem.item_id) });
                }
            }
            else
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
                }
            }
            return null;
        }

        /// <summary>
        /// Klasa pomocnicza do generowania linkow zwiazanych z routem <code>MenusList</code>.
        /// </summary>
        /// <param name="urlHelper">url helper z c#</param>
        /// <param name="menu">aktualne menu</param>
        /// <param name="menuItem">atualna pozycja menu</param>
        /// <param name="language">aktualna wersja jezykowa</param>
        /// <returns></returns>
        public static string GenerateToUpMenusListRoute(this UrlHelper urlHelper, Menu menu, MenuItem menuItem,
            Language language)
        {
            int page = 1;
            if (menu == null)
            {
                MenuDAO menuDAO = new MenuDAO();
                menu = menuDAO.GetDefaultMenu(language);
            }   //if menu 
            int itemParentId = 0;
            if (menuItem != null)
            {
                itemParentId = menuItem.parent_item_id ?? 0;
            }   //if page type


            if (itemParentId != 0)
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusList", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id), menuItemId = (itemParentId) });
                }
            }
            else
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
                }
            }
            return null;

        }

        /// <summary>
        /// Klasa pomocnicza do generowania linkow zwiazanych z routem <code>MenusList</code>.
        /// </summary>
        /// <param name="urlHelper">url helper z c#</param>
        /// <param name="menu">atualne menu</param>
        /// <param name="language">aktualna wersja jezykowa</param>
        /// <returns></returns>
        public static string GenerateMenusListRouteAndDelete(this UrlHelper urlHelper, Menu menu, MenuItem menuItem,
            Language language)
        {
            int page = 1;
            if (menu == null)
            {
                MenuDAO menuDAO = new MenuDAO();
                menu = menuDAO.GetDefaultMenu(language);
            }   //if menu 
            int menuItemId = 0;
            int parentMenuItemId = 0;
            if (menuItem != null)
            {
                menuItemId = menuItem.item_id;
                parentMenuItemId = menuItem.parent_item_id ?? 0;
            }   //if page type
            if (menuItemId != 0)
            {
                if (DeleteMenuItemRecursive(menuItem))
                {
                    if ((language != null) && (menu != null) && (DeleteMenuItemRecursive(menuItem)))
                    {
                        if (parentMenuItemId != 0)
                        {
                            return urlHelper.RouteUrl("MenusList", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id), menuItemId = (parentMenuItemId) });
                        }
                        else
                        {
                            return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
                        }
                    }
                }
                else
                {
                    Logger.MyLogger.Log("MenuRoutesHelper exception: GenerateMenusListRouteAndDelete method, delete on cancel failed...");
                    return null;
                }
            }
            else
            {
                if ((language != null) && (menu != null))
                {
                    return urlHelper.RouteUrl("MenusListNoMenuItem", new { page = (page), currentLanguageId = (language.language_id), menuId = (menu.menu_id) });
                }
            }
            return null;
        }

        /// <summary>
        /// Metoda pomocnicza usuwajaca galaz drzewa.
        /// </summary>
        /// <param name="menuItem">rodzic, od ktorego zaczynamy usuwanie</param>
        /// <returns>true, gdy sie uda, false wpp</returns>
        private static bool DeleteMenuItemRecursive(MenuItem menuItem)
        {
            MenuItemDAO menuItemDAO = new MenuItemDAO();
            try
            {
                if (menuItem == null)
                {
                    throw new Exception("MenuRoutesHelper exception: DeleteMenuItemRecursive method, menuItem object null...");
                }   //if null
                IQueryable<MenuItem> childMenuItems = menuItemDAO.FindByParentId(menuItem.item_id);
                if (childMenuItems != null)
                {
                    foreach (MenuItem childMenuItem in childMenuItems)
                    {
                        DeleteMenuItemRecursive(childMenuItem);
                    }   //foreach
                }   //if not null
                menuItemDAO.Delete(menuItem.item_id);
                return menuItemDAO.SubmitChanges();
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return false;
            }   //catch
        }

    }
}
